<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Trade_Template extends Controller_Admin_Main
{

    private $trade_template;

    public function before()
    {
        parent::before();
        Assets::js('tinymce', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');
        $this->trade_template = Model::factory('trade_template');
    }

    //供求分类模板列表
    public function action_list()
    {
        $this->display_list();
    }

    //搜索
    public function action_search()
    {
        $data = array();
        $data['name'] = Arr::get($_POST, 'keywords', '');
        $this->display_list($data);
    }

    public function display_list($data = '')
    {
        $template_list = $this->trade_template->get_trade_template_list($data);
        //获取供求需要的分类
        $category_option = $this->get_category_cache_option('trade');
        $this->template->assign('category_option', $category_option);

        $this->template->assign('template_list', $template_list['list']);
        $this->template->assign('page', $template_list['page']);
        $this->template->view('trade_template_list');
    }

    //添加供求分类的某一个模板
    public function action_add()
    {
        if ($_POST)
        {
            $this->save_template($_POST);
        }
        //获取供求需要的分类
        $category_option = $this->get_category_cache_option('trade');
        $this->template->assign('category_option', $category_option);
        $this->template->view('trade_template_add');
    }

    //修改某个供求分类的模板
    public function action_edit()
    {
        $id = $this->request->param('id');

        $detail = $this->trade_template->get_row(array('id' => $id));

        if ($_POST)
        {
            $_POST['id'] = $id;
            $this->save_template($_POST);
        }

        //获取供求需要的分类
        $category_option = $this->get_category_cache_option('trade', $detail['category_id']);
        $this->template->assign('category_option', $category_option);
        $this->template->assign('data', $detail);
        $this->template->view('trade_template_edit');
    }

    //删除某个供求分类的模板
    public function action_delete()
    {
        $id = $this->request->param('id');

        if ($this->trade_template->delete(array('id' => $id)))
        {
            Message::success(__('delete success'));
            $this->request->redirect('admin/trade/template/list');
        }
    }

    public function save_template($post)
    {
        $id = isset($post['id']) ? $post['id'] : '';

        $data = array();
        $data['category_id'] = Arr::get($post, 'category_id', 0);
        $data['position'] = Arr::get($post, 'position', 9999);
        $data['content'] = Arr::get($post, 'content', '');
        $insert_id = $this->trade_template->insert_trade_template($data);
        if (!empty($id))
        {
            Message::success('修改成功');
            $this->request->redirect('admin/trade/template/edit/' . $insert_id);
        }
        Message::success('添加成功');
        $this->request->redirect('admin/trade/template/add');
    }

}